<?php
namespace app\index\controller;
use think\Controller;
use think\Validate;
use think\Db;
use think\Session;
class User extends Sessiond
{  

    public function index()
    {
     $date=[];
      $date['user']=userarray();
	  $id=$date['user']['id'];
	  $date['sjx']=sumnews($id);//收件箱
	  $date['sumtg']=Db::name('member')->order('id desc')->field('name,id')->where('inviter',$id)->count();
	  
	  $this->assign($date);
     return $this->fetch();
    }

	public function userindex(){
	     $date['user']=userarray();$id=$date['user']['id'];$arr['userid']=$id;
		 $date['sumorder']=sumorderlist();
		 $date['zczmoney']=sumzczmoney();
		 
		 $date['countorder']=Db::name('orderlist')->where($arr)->count();
		 $date['countorderf']=Db::name('orderlist')->where("userid='".$id."' and stater='1'")->count();
		 $date['tzsy']=sumorderfh($id);
		 $date['bjhs']=sumorderbj($id);
		   $this->assign($date);
	     return $this->fetch();
	}
	public function msg()
    {
		   $user=userarray();
		 $post=input("post.");//获取post数据'
	  if($post){
	     
		// return json($post);
		$date['msg']='';$date['status']=0;
		$fin= Db::name('news')->where("id",$post['id'])->find();
		
		if(!$fin){$date['msg']='无此短信';}
		if($fin['uid']!=$user['id']){$date['msg']='无权操作';}
		if($post['type']=='1'){//月都
		
		
		if($fin['stater']=='1'){ $date['status']=1;$date['msg']='1';$date['sx']='0';}
		if($date['msg']==''){
		  $date['status']=1;$date['sx']='1';
		  Db::name('news')->where("id",$post['id'])->update(array("stater"=>'1'));
		  $date['html']=msgstater(1);
		  
		}
		}else{//删除
		 if($date['msg']==''){
		
		 Db::name('news')->where("id",$post['id'])->delete();
		  $date['status']=1;$date['sx']='1';$date['msg']='删除成功';
		 }
		  
		}
		return json($date);
	  
	  }else{
   
	  
	  $news= Db::name('news')->where(" uid='".$user['id']."'")->order('id desc')->paginate(10);
	  $this->assign('new',$news);
	   $this->assign('user',$user);
     return $this->fetch();
	 
	 }
    }

	public function memberlog(){
	   $user=userarray();
	 $news= Db::name('memberlog')->where(" userid='".$user['id']."'")->order('id desc')->paginate(10);
	  $this->assign('new',$news);
	   $this->assign('user',$user);
     return $this->fetch();
	}

	public function qiandao(){//签到
	 $post=input("post.");//获取post数据'
	  if($post){
		  $userid=Session::get('userid');$date['msg']='';$date['status']='0';
	      $find=Db::name('moneylog')->where("userid='".$userid."' and addtime>='".date("Y-m-d")."' and notice like '%签到奖励%'")->find();
		  if($find){$date['msg']='今天已经签到了，明天再来！';}
		   $config= Db::name('config')->find();
				  if($config['qdje']>0){
				   $money=$config['qdje'];
				  }else{
				    $date["msg"]="未设置签到奖励，无法签到！";
				  }
		  if($date['msg']==''){
			  Db::startTrans();//事物开始
		     try{
				 
		      if($money>0){
				  $kk= Db::name('member')->where('id',$userid)->setInc('amount',$money);
				  if($kk){ addmoneylog($userid,'签到奖励',$money,'+');//添加金额记录
				  }
				  $user=userarray();
				  $ab="尊敬的".$user['name']."会员您好！恭喜您签到成功，获得签到礼包：".$money."元";
				  $con="签到礼包".$money."元";
				  addnews($ab,$con,$userid);
			  }
               $date['msg']="签到成功！";
			   $date['status']="1";
			    Db::commit();//提交事件
          
      
	         }catch(\Exception $e) {
			  
			  
			  // 回滚事务
			  
			   Db::rollback();
			   $date['msg']='未知错误,数据已回滚！';
			 
			 }

		  }

		  return json($date);
	  }
	
	}
	public function usercz()
    {//充值
       $pay=Db::name('pay')->select();
	   $this->assign('pay',$pay); 
     return $this->fetch();
    }
  
	

	public function moneylog()
    {//金额记录
     $userid=Session::get('userid');
	 $log=Db::name('moneylog')->order('id desc')->where('userid',$userid)->paginate(5);
      $this->assign("log",$log);
     return $this->fetch();
    }
	
	
	
	
	public function userzlxg()
    {
		$post=input("post.");
			       
		if($post){
			
	      
         
		   $id=Session::get('userid');
		   $error['stater']='0';
		   $error['msg']='';
           $user= Db::name('member')->where('id',$id)->find();
		    if(!empty($post['card']) and $user['card']==''){
		     $date['card']=$post['card'];
		    }
			if(!empty($post['realname']) and $user['realname']==''){
		     $date['realname']=$post['realname'];
		    }
			$xg='';
			if(!empty($post['bankname']) and $user['bankname']==''){
		     $date['bankname']=$post['bankname'];$xg='1';
		    }
			if(!empty($post['bankcode']) and $user['bankcode']==''){
		     $date['bankcode']=$post['bankcode'];$xg='1';
		    }
			if(!empty($post['bankaddress']) and $user['bankaddress']==''){
		     $date['bankaddress']=$post['bankaddress'];
              $xg='1';
		    }
          if(!empty($post['bankrealname']) and $user['bankrealname']==''){
		     $date['bankrealname']=$post['bankrealname'];
              $xg='1';
		    }
		
			if(!empty($post['newtel']) and $user['mobile']!=''){
		     $date['mobile']=$post['newtel'];
              $us=Db::name('member')->where('mobile',$post['newtel'])->find();
			  if($us)$error['msg']='新手机已经绑定！';
              $xg='1';
		    }
		    if($xg!=''){

//		      if($post['yzm']!=Session::get('yzm') or $post['yzm']==''){
//		       exit(json_encode(array('status'=>'0','msg'=>'验证码错误！')));
//		       }
			  
		    }
			if(!empty($post['newwenti']) and  !empty($post['newdaan']) and $user['mbwt']==''){
			    
			  $date['mbwt']=$post['newwenti'];$date['mbwtda']=$post['newdaan'];
			}

			if(!empty($post['newwenti']) and  !empty($post['newdaan']) and $user['mbwt']!='' and $user['mbwtda']!='' ){
			    
			   if($post['wenti']!=$user['mbwt'])$error['msg']='旧安全问题错误！';
			   if($post['danan']!=$user['mbwtda'])$error['msg']='旧安全问题答案错误！';
			 
			   if($error['msg']==''){
			   $date['mbwt']=$post['newwenti'];$date['mbwtda']=$post['newdaan'];
			   }
			}


		  if(!empty($date) and $error['msg']==''){ 
             Db::name('member')->where('id',$id)->update($date);
		     $error['stater']='1';
			 $error['msg']='修改成功';
			 if($xg!='') Session::set($user['mobile'],'');
			 }else{
			  if($error['msg']=='')$error['msg']='修改失败';
			  
			 }
		   return json($error);
		
		}else{
      $user=userarray();
	  $this->assign('user',$user);
	  
     return $this->fetch();
	   }
    }
	
	public function pwdup()
    {

    
		$post=input("post.");
			       
		if($post){
			
	      
         
		   $user=userarray();
		   if(!$user){ exit(json_encode(array('status'=>'0','msg'=>'账号错误')));}
		  if(md5($post['oldpwd'])!=$user['pwd']){exit(json_encode(array('status'=>'0','msg'=>'原密码错误')));}
		  if($post['newpwd']==''){exit(json_encode(array('status'=>'0','msg'=>'新密码不能为空')));}
		  if($post['newpwd']!=$post['pwd2']){exit(json_encode(array('status'=>'0','msg'=>'2次密码不一致')));}
		   $date['pwd']=md5($post['newpwd']);
           $db=Db::name('member')->where('id',$user['id'])->update($date);
		   if($db)
		    exit(json_encode(array('status'=>'1','msg'=>'修改成功')));
		   else 
			   exit(json_encode(array('status'=>'1','msg'=>'修改失败')));
		   
		
		}else{
     
	        $user=userarray();
	  $this->assign('user',$user);
            return $this->fetch('userpwd');
	   }
    }
	public function pwd2up()
    {

    
		$post=input("post.");
			       
		if($post){
			
	      
         
		   $user=userarray();
		   if(!$user){ exit(json_encode(array('status'=>'0','msg'=>'账号错误')));}
		  if(md5($post['oldpwd'])!=$user['paypwd']){exit(json_encode(array('status'=>'0','msg'=>'原交易密码错误')));}
		  if($post['newpwd']==''){exit(json_encode(array('status'=>'0','msg'=>'新交易密码不能为空')));}
		  if($post['newpwd']!=$post['pwd2']){exit(json_encode(array('status'=>'0','msg'=>'2次密码不一致')));}
		  if($post['yzm']!=Session::get($user['mobile'])){
		  exit(json_encode(array('status'=>'0','msg'=>'验证码错误！')));
		  }
		   $date['paypwd']=md5($post['newpwd']);Session::set($user['mobile'],'');
           $db=Db::name('member')->where('id',$user['id'])->update($date);
		  
		   if($db)
		    exit(json_encode(array('status'=>'1','msg'=>'修改成功')));
		   else 
			   exit(json_encode(array('status'=>'1','msg'=>'修改失败')));
		   
		
		}else{
     
	        $user=userarray();
	  $this->assign('user',$user);
            return $this->fetch();
	   }
    }
	public function pwd2zh()
    {//召回交易密码

    
		$post=input("post.");
			       
		if($post){
			
	      
         
		   $user=userarray();
		   if(!$user){ exit(json_encode(array('status'=>'0','msg'=>'账号错误')));}
		  
		  if($post['newpwd']==''){exit(json_encode(array('status'=>'0','msg'=>'新交易密码不能为空')));}
		  if($post['newpwd']!=$post['pwd2']){exit(json_encode(array('status'=>'0','msg'=>'2次密码不一致')));}
		  if($post['yzm']!=Session::get($user['mobile'])){
		  exit(json_encode(array('status'=>'0','msg'=>'验证码错误！')));
		  }
		   $date['paypwd']=md5($post['newpwd']);Session::set($user['mobile'],'');
           $db=Db::name('member')->where('id',$user['id'])->update($date);
		  
		   if($db)
		    exit(json_encode(array('status'=>'1','msg'=>'修改成功')));
		   else 
			   exit(json_encode(array('status'=>'1','msg'=>'修改失败')));
		   
		
		}else{
     
	        $user=userarray();
	  $this->assign('user',$user);
            return $this->fetch();
	   }
    }
    
	public function bankbd(){//绑定银行卡
		$post=input("post.");
		 $user=userarray();	
		 if($user['realname']=='' or $user['card']==''){
		  echo "<script>alert('资料未完善！');window.location.href='/index/user/userzlxg'</script>";
		 }
		
     
	       
	  $this->assign('user',$user);
            return $this->fetch();
	  
	
	}
     public function orderlistlog(){//
	    $date['type']=input("param.type");
		$type=$date['type'];
		if($type!=3&&$type!=6){
		    $this->error('访问失败');exit();
        }
        if($type==3){
            $map['type'] = 6;
        }
         if($type==6){
             $map['type'] = 2;
         }
		$date['user']=userarray();
		$id=$date['user']['id'];
        $map['userid'] = $id;

        $date['log']= Db::name('moneylog')->where($map)->order('id desc')->paginate(10);
        $date['ztz']=-Db::name('moneylog')->where($map)->sum('money');
		$this->assign($date);
	  return $this->fetch('fhmx');
	 }
	 public function usermoney()
    {

     $date['user']=userarray();$id=$date['user']['id'];	
      $date['type']=input("param.type");
	  $type=$date['type'];
      if($type=="1"){
	   $date['qrtx']=Db::name('txlist')->where("userid='".$id."' and state ='0'")->sum('money');
	   $date['qrcz']=Db::name('paylist')->where("userid='".$id."' and state ='0'")->sum('money');
	   $date['ljtx']=Db::name('txlist')->where("userid='".$id."' ")->sum('money');
	   $date['ljcz']=Db::name('paylist')->where("userid='".$id."' ")->sum('money');
	   $date['ljtz']=Db::name('orderlist')->where("userid='".$id."' ")->sum('money');
	  }
	  if($type=='2'){
	   $date['pay']=Db::name('pay')->where("state='1' ")->select();
	  }
	  if($type=='3'){
	   $date['paylist']=Db::name('paylist')->where(" userid='".$id."' ")->order('id desc')->paginate(10);
	   $pay=Db::name('pay')->select();
	   foreach($pay as $va){
	     $arr[$va['id']]=$va['zwname'];
	   }
	   $date['pay']=$arr;
	   $date['sumcg']=Db::name('paylist')->where("state='1' and userid='".$id."' ")->sum('money');
	   $date['sumsb']=Db::name('paylist')->where("state='2' and userid='".$id."' ")->sum('money');
	   $date['sumwcl']=Db::name('paylist')->where("state='0' and userid='".$id."' ")->sum('money');
	  }
	  if($type=='4'){
		 if($date['user']['bankcode']=='' ){
		  echo "<script>alert('银行卡未绑定');window.location.href='/index/user/bankbd'</script>";
		 }
	  }
	  if($type==5){
	   $date['txlist']=Db::name('txlist')->where(" userid='".$id."' ")->order('id desc')->paginate(10);
	    $date['sumcg']=Db::name('txlist')->where("state='1' and userid='".$id."' ")->sum('money');
	    $date['sumsb']=Db::name('txlist')->where("state='2' and userid='".$id."' ")->sum('money');
	    $date['sumwcl']=Db::name('txlist')->where("state='0' and userid='".$id."' ")->sum('money');
	  }
        if($type==6){
            $map['userid'] = $id;
            $map['type'] = 4;
            $date['list']= Db::name('moneylog')->where($map)->order('id desc')->paginate(10);
            $date['money']=Db::name('moneylog')->where($map)->sum('money');
        }
      $this->assign($date);
     return $this->fetch();
    }
    public function inviter(){
	  $date['user']=userarray();$id=$date['user']['id'];	
      $date['type']=input("param.type");
	  $type=$date['type'];
	  if($type=='1'){
		  $config= Db::name('config')->find();
		  $url="http://".$config['tgurl']."/index/index/reg/id/".$date['user']['id'];
	      $img=md5($url). ".png";;
	      $date['urlf']= "/ewm/".$img;
		  $date['url']=$url;
	  }
	  if($type=='2'){
       $param=input('param.');
	   $id=!empty($param['id'])?$param['id']:$id;
	   $date['ii']=!empty($param['ii'])?$param['ii']+1:1;
	   $date['log']=Db::name('member')->order('id desc')->field('name,id,regtime')->where('inviter',$id)->paginate(10);
       $xjarr=Db::name('member')->order('id desc')->field('name,id,regtime')->where('inviterarr like "%'.$date['user']['id'].'%"')->select();
       $idf="";
      foreach($xjarr as $va){
        $idf.=$va['id'].",";
      }
      $idf=trim($idf,','); $date['sumxjtx']=0;
      $date['sumxjcz']=0;
      if($idf!=''){
       $date['sumxjcz']=Db::name('paylist')->where("state='1' and userid in(".$idf.")")->sum('money');
       $date['sumxjtx']=Db::name('txlist')->where("state='1' and userid in(".$idf.")")->sum('money');
      }
	  }
    if($type=='3'){
        $map['userid'] = $id;
        $map['type'] =2;
        $date['log']= Db::name('moneylog')->where($map)->order('id desc')->paginate(10);
        $date['ztz']=-Db::name('moneylog')->where($map)->sum('money');
    }
	   $this->assign($date);
       return $this->fetch();
	}
	
	public function wdtd()
    {//推荐记录
		 $user=userarray();
    $param=input('param.');
	$id=!empty($param['id'])?$param['id']:$user['id'];
	 $log=Db::name('member')->order('id desc')->field('name,id')->where('inviter',$id)->paginate(10);
	
	
      $this->assign("log",$log);
	 
     return $this->fetch();
    }

    public function useraqzs()
    {
	   $date['user']=userarray();$id=$date['user']['id'];	
      $date['type']=input("param.type");
	  $type=$date['type'];
	   $this->assign($date);
     return $this->fetch();
	
	
	}

    public function userft()
    {//复投
        $post=input("post.");//获取post数据'
        if($post){
            $date['money']=$post['money'];
            $config= Db::name('config')->find();
            if(  empty($date['money'])){exit(json_encode(array('status'=>'0','msg'=>'金额不能为空')));}
            if($date['money']<$config['ftmin'] ){exit(json_encode(array('status'=>'0','msg'=>'复投金额最低为'.$config['ftmin'])));}
            $userid=Session::get('userid');
            $user=Db::name('member')->where('id',$userid)->find();
            if(!$user){exit(json_encode(array('status'=>'0','msg'=>'账户不存在')));}
            if($user['paypwd']!=MD5($post['paypwd'])){exit(json_encode(array('status'=>'0','msg'=>'交易密码错误！')));}
            if($user['money']<$date['money']){exit(json_encode(array('status'=>'0','msg'=>'积分不足')));}
            $fee = $post['money']*$config['ftfee'];

            handlelog($userid,($post['money']-$fee)*$config['ftbs'],'+',3,'复投');
            handlelog($userid,$post['money'],'-',4,'复投');
            exit(json_encode(array('status'=>'1','msg'=>'复投成功')));
        }
    }
	public function usertx()
    {//提现

		  $post=input("post.");//获取post数据'
		   if($post){
		   //exit(json_encode(array('status'=>'0','msg'=>$post)));
		   $date['money']=$post['money'];
		    $config= Db::name('config')->find();
		   if(  empty($date['money'])){exit(json_encode(array('status'=>'0','msg'=>'金额不能为空')));}
		    if($date['money']<$config['czmin'] ){exit(json_encode(array('status'=>'0','msg'=>'提现金额最低为'.$config['czmin'])));}
		    $userid=Session::get('userid');
		   $user=Db::name('member')->where('id',$userid)->find();
		   if(!$user){exit(json_encode(array('status'=>'0','msg'=>'账户不存在')));}
		   if($user['paypwd']!=MD5($post['paypwd'])){exit(json_encode(array('status'=>'0','msg'=>'交易密码错误！')));}
		   if($user['bankcode']==''){exit(json_encode(array('status'=>'1','msg'=>'资料未完善','url'=>'/index/user/userzlxg'))); die;}
		   if($user['money']<$date['money']){exit(json_encode(array('status'=>'0','msg'=>'积分不足')));}
		   $date['card']=$user['bankcode'];
		   $date['true_money']=$post['money']*(1-$config['txfee']);
		   $date['name']=$user['bankrealname'];
           $date['bank']=$user['bankname'];
           $date['zhihang']=$user['bankaddress'];
		   $date['addtime']=date("Y-m-d H:i:s");
		   $date['userid']=$userid;
		   $date['ylph']=$user['mobile'];
		   $insert=Db::name('txlist')->insert($date);
		   if($insert){

               handlelog($userid,$post['money'],'-',7,'申请提现');
			   exit(json_encode(array('status'=>'1','msg'=>'提现成功,等待审核')));
			   }
			else{ exit(json_encode(array('status'=>'0','msg'=>'未知错误')));}
		  }
		  else{

	     }
    }
	
}
